package com.srd.phone.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.shengrongdai.srd.R;
import com.srd.phone.application.AppActivityManager;
import com.srd.phone.bean.ResultInfo;
import com.srd.phone.mvp.presenters.RetrievePwdResetPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.LogUtils;

/**
 * 找回密码，二次输入密码
 */
public class RetrievePwdResetActivity extends BaseActivity implements View.OnClickListener {

    private RetrievePwdResetPresenter retrievePwdResetPresenter;
    private EditText et_pwd;
    private EditText et_check_pwd;
    private TextView tv_warn;
    private Button bt_submit;

    private String url = Constants.BASEURL + "loginapp/setNewPwd";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve_reset_pwd);

        initView();
        initData();
    }

    @Override
    public void initView() {
        tv_common_title.setText("重置登录密码");
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        et_check_pwd = (EditText) findViewById(R.id.et_check_pwd);
        tv_warn = (TextView) findViewById(R.id.tv_warn);
        bt_submit = (Button) findViewById(R.id.bt_submit);
        bt_submit.setOnClickListener(this);
    }

    @Override
    public void initData() {
        presenter = new RetrievePwdResetPresenter(requestQueue, this);
        retrievePwdResetPresenter = (RetrievePwdResetPresenter)presenter;
        setUrlArrs(url);
    }

    //提交密码
    private void submitPwd() {
        String pwd = et_pwd.getText().toString().trim();
        String check_pwd = et_check_pwd.getText().toString().trim();
        String value = retrievePwdResetPresenter.checkedPwd(pwd, check_pwd);
        if (TextUtils.isEmpty(value)) {
            tv_warn.setVisibility(View.INVISIBLE);
            retrievePwdResetPresenter.requestSubmit(url, pwd);
        } else {
            tv_warn.setVisibility(View.VISIBLE);
            tv_warn.setText(value);
        }

    }

    @Override
    public void returnData(String tag, String data) {
        LogUtils.i(RetrievePwdResetActivity.class, data);
        ResultInfo info = ResultInfo.parse(data);
        if (info.code == -101) {
            Toast.makeText(RetrievePwdResetActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
            AppActivityManager.getInstance().popActivity();
            AppActivityManager.getInstance().popActivity();
        } else {
            tv_warn.setText(info.msg);
            tv_warn.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_submit:
                submitPwd();
                break;
        }
    }

}
